<template>
  <!-- 数据报表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="报表目录" prop="directoryId">
            <el-tree-select
              style="width: 100%"
              v-model="formData.directoryId"
              placeholder="选择数据报表目录"
              :data="directoryList"
              filterable
              check-strictly
              :render-after-expand="false"
              :props="defaultProps"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="是否启用" prop="enabled">
            <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.enabled"
              placeholder="请选择是否启用"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('Enabled')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName("Enabled", formData.enabled)
            }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="报表编号" prop="reportCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.reportCode"
              placeholder="请输入报表编号"
              clearable
            />

            <span v-else>{{ formData.reportCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报表名称" prop="reportName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.reportName"
              placeholder="请输入报表名称"
              clearable
            />

            <span v-else>{{ formData.reportName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="报表地址" prop="reportUrl">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.reportUrl"
              placeholder="请输入报表地址"
              clearable
            />

            <span v-else>{{ formData.reportUrl }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="报表描述" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              type="textarea"
              v-model="formData.remarks"
              placeholder="请输入报表描述"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "DMPDATAREPORTINFOADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    if (newVal) getdirectory();
  }
);

const rules = reactive({
  directoryId: [
    { required: true, message: "选择数据报表目录", trigger: "blur" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  reportCode: [
    { required: true, message: "请输入报表编号", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  reportName: [
    { required: true, message: "请输入报表名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  reportUrl: [
    { required: true, message: "请输入报表地址", trigger: "blur" },
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  enabled: [{ required: true, message: "请选择是否启用", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  directoryId: "",
  directoryIdList: [],
  reportCode: "",
  reportName: "",
  reportUrl: "",
  remarks: "",
  enabled: "",
  deleted: "",
  createdUserId: "",
  createTime: "",
  lastUpdatedUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        directoryId,
        directoryIdList,
        reportCode,
        reportName,
        reportUrl,
        remarks,
        enabled,
        deleted,
        createdUserId,
        createTime,
        lastUpdatedUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        directoryId,
        directoryIdList,
        reportCode,
        reportName,
        reportUrl,
        remarks,
        enabled,
        deleted,
        createdUserId,
        createTime,
        lastUpdatedUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.directoryId = "";
  formData.directoryIdList = [];
  formData.reportCode = "";
  formData.reportName = "";
  formData.reportUrl = "";
  formData.remarks = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.createdUserId = "";
  formData.createTime = "";
  formData.lastUpdatedUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 数据报表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/dmpAdmin/dmpdatareportinfo/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const directoryList = ref([]);

function getdirectory() {
  request({
    url: `/dmpAdmin/dmpdatareportdirectory/gettrees`,
    method: "get",
    data: {},
  })
    .then((res) => {
      directoryList.value = res.data;
    })
    .catch((res) => {
      console.log(res);
    });
}

const defaultProps = ref({
  children: "nodes",
  label: "directoryName",
  value: "id",
});

function handleNodeClick(item) {
  formData.directoryId = item.id;
  console.log(formData.directoryId);
}
</script>
